package com.jxLC.Service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class UploadService {

	/*
	 * 将word文档转换成htm文 档
	 */
     public  void wordToHtm(String filepaths, String savepaths) {
		   @SuppressWarnings("unused")
		boolean flag = false;
		   ComThread.InitSTA();  
		   ActiveXComponent app = new ActiveXComponent("Word.Application"); 
		   String inFile =filepaths;
		   String tpFile =savepaths;
		   System.out.println("infile="+inFile);
		   System.out.println("tpFile="+tpFile);
		   try {
		    app.setProperty("Visible", new Variant(false));
		    Dispatch docs = app.getProperty("Documents").toDispatch();
		    Dispatch doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] { inFile, new Variant(false),new Variant(true) },
		       new int[1]).toDispatch();    
		    Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {tpFile, new Variant(8) }, new int[1]);
			Dispatch.call(doc, "Close", new Variant(false));
		    flag = true;
		   } catch (Exception e) {
		    e.printStackTrace();
		   } finally {
		    app.invoke("Quit", new Variant[] {});
		    ComThread.Release();   
		   }
	 }
     
     /*
      * 单个上传
      */
     public String upload(String filepath,File uplaodFile)  {
		try {
			FileOutputStream fos;
			fos = new FileOutputStream(filepath);
			FileInputStream fis=new FileInputStream(uplaodFile);
	 		byte[] buffer=new byte[1024];
	 		int len=0;
	 		while((len=fis.read(buffer))>0){
	 			fos.write(buffer,0,len);
	 		 }
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return  "success";
     }
     
     /*
      * 上传图片
      */
     public String batchUpload(String realpath,File image,String imageName,int index){
	          this.existDir(realpath);
	    	  int t=imageName.lastIndexOf(".");
	    	  String suffixName=imageName.substring(t);
	    	  String imagepath=realpath+"\\"+index+suffixName;
	    	  File temp=new File(imagepath);
	    	  temp.delete();
	    	  if(this.upload(imagepath, image).equals("error")){
					return "error";
		      }
	       
    	 return "success";
     }
     
     
     //检查文件夹upload是否存在
     public void existDir(String realpath){
    	 File   dirname   =   new   File(realpath); 
 		if   (!dirname.exists())  
 		{ 
 		dirname.mkdir();
 		} 

     }
     
     //取得htm文件内容
     public String getHtmContent(String htmpath){ 
    	 try {
    		    String text=null;
	            FileReader fr = new FileReader(htmpath);
	            BufferedReader br = new BufferedReader(fr);    
	            String myreadline;   
	            while (br.ready()) {
	                myreadline = br.readLine();
	                text=text+myreadline;
	            }
	            br.close();
	            fr.close();
	            return text.substring(4);

	        } catch (IOException e) {
	            e.printStackTrace();
	            return "error";
	        }
    	
     }
     
     public  void delFolder(String folderPath) {
         try {
            delAllFile(folderPath); //删除完里面所有内容
            String filePath = folderPath;
            filePath = filePath.toString();
            java.io.File myFilePath = new java.io.File(filePath);
            myFilePath.delete(); //删除空文件夹
         } catch (Exception e) {
           e.printStackTrace(); 
         }
    }

    
     public  boolean delAllFile(String path) {
         boolean flag = false;
         File file = new File(path);
         if (!file.exists()) {
           return flag;
         }
         if (!file.isDirectory()) {
           return flag;
         }
         String[] tempList = file.list();
         File temp = null;
         for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
               temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
               temp.delete();
            }
            if (temp.isDirectory()) {
               delAllFile(path + "/" + tempList[i]);
               delFolder(path + "/" + tempList[i]);
               flag = true;
            }
         }
         return flag;
       }
     
     /*
      * 取得上传的word文档的带格式的内容
      */
      public String setText(String realpath,File upload,String uploadFileName){
    	String filepath=realpath+"\\"+uploadFileName;   //filepath 是word文档上传后的保存的路径
    	
    	System.out.println("filePath="+filepath);
  		this.existDir(realpath);
  		
  		if(this.upload(filepath,upload).equals("error")){
  		//	this.setErrorMsg("文件上传出错");
  			return "error";
  		}
  		 
  		int index=filepath.lastIndexOf(".");
  		String htmpath=filepath.substring(0,index)+".htm";	//  htmpath是word转换成的htm文件的路径
  		this.wordToHtm(filepath, htmpath);
  		String text=this.getHtmContent(htmpath);
  		this.delAllFile(realpath);
  		return text;
      }
     
}



     
     

